//
//  HEMyDocTeamViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/5/5.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEMyDocTeamViewController.h"
#import "HETeamModel.h"
#import "HEFindTeamCell.h"
#import "HEFindServerMenuView.h"
#import "HEFindFiterModel.h"
#import "HETeamFilterModel.h"
#import "HETeamDetailsViewController.h"
#import "HEIMTeamViewController.h"

@interface HEMyDocTeamViewController ()<UITableViewDelegate, UITableViewDataSource, HEFindServerMenuViewDelegate, QMUITextFieldDelegate>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, strong) NSString *departName;
@property (nonatomic, strong) NSString *hospitalName;


@property (nonatomic, strong) QMUITextField *searchTF;
@property (nonatomic, strong) HEFindServerMenuView *menuView;
@property (nonatomic, strong) NSMutableArray <HETeamModel *> * dataSourceArr;

@property (nonatomic, strong) NSMutableArray *menuData;

@end

@implementation HEMyDocTeamViewController

- (NSMutableArray<HETeamModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的团队";
    self.zx_navLineView.hidden = YES;
    
    _pageNum = 1;
    _pageSize = 10;
        
    [self initUI];

    [self.view addSubview:self.tableView];
    [self setCustomTableView:self.tableView];
    [self showEmpty];
 
    [self loadData];
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollsToTop = YES;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = KWhiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        __weak __typeof(&*self)weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.isFromHeadRefresh = YES;
            weakSelf.pageNum = 1;
            [weakSelf loadData];
        }];

        [_tableView registerClass:[HEFindTeamCell class] forCellReuseIdentifier: @"HEFindTeamCell"];
    }
    return _tableView;
}

- (void)initUI {
    // 搜索
    UIView *searchWarp = [UIView new];
    searchWarp.backgroundColor = KWhiteColor;
    [self.view addSubview:searchWarp];
    [searchWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).mas_offset(ZXNavBarHeight);
        make.left.right.equalTo(self.view);
        make.height.mas_offset(50);
    }];
    
    searchWarp.qmui_borderPosition = QMUIViewBorderPositionBottom;
    searchWarp.qmui_borderWidth = 1;
    searchWarp.qmui_borderColor = CSplitLineColor;
    
    // 输入框
    QMUITextField *searchTF = [QMUITextField new];
    _searchTF = searchTF;
    [searchWarp addSubview:searchTF];
    searchTF.backgroundColor = CViewBgColor;
    
    searchTF.placeholder = @"搜索专家团队";
    searchTF.placeholderColor = [UIColor colorWithHexString:@"#AAAAAA"];
    searchTF.delegate = self;
    searchTF.layer.cornerRadius = 20;

    UIView *leftWarp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 49, 18)];
    UIImageView *leftIcon = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"home_search")];
    leftIcon.frame = CGRectMake(20, 0, 18, 18);
    [leftWarp addSubview:leftIcon];
    searchTF.leftViewMode = UITextFieldViewModeAlways;
    searchTF.leftView = leftWarp;
 
    UIButton *searBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searBtn setTitleColor:CNavBgColor forState:UIControlStateNormal];
    searBtn.titleLabel.font = DYFont(17);
    [searchWarp addSubview:searBtn];
    [searBtn addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
    
    [searBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(searchWarp.mas_top);
        make.right.equalTo(searchWarp.mas_right).mas_offset(-15);
        make.height.mas_offset(40);
        make.width.mas_offset(60);
    }];
    
    [searchTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(searchWarp.mas_top);
        make.left.equalTo(searchWarp.mas_left).mas_offset(15);
        make.right.equalTo(searBtn.mas_left).mas_offset(-10);
        make.height.mas_offset(40);
    }];
}

- (void)searchClick {
    [_tableView.mj_header beginRefreshing];
}

- (HEFindServerMenuView *)menuView
{
    if (!_menuView) {
        _menuView = [[HEFindServerMenuView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight + 50, KScreenWidth, 50) maxHeight:CGRectGetHeight(self.view.frame) - 50 - ZXNavBarHeight - 50 menuType:HEFilterMenuTypeFindTeam];
        _menuView.delegate = self;
    }
    return _menuView;
}

#pragma mark -

- (void)loadChooseData {
    [WOAFNetWorkingHandle GETWithBaseUrl:KServerUrl suffixUrl:KTeamFilter parameters:@{} success:^(id  _Nonnull responseObject) {
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        self.menuData = [HETeamFilterModel mj_objectArrayWithKeyValuesArray:response.data];
        [self dealFiterData];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)dealFiterData {

    NSMutableArray *depts = @[].mutableCopy;
    
    HEFindFiterModel *allItem = [HEFindFiterModel new];
    allItem.name = @"全部";
    [depts addObject: allItem];
      
   
    // 筛选医院下的科室
    for (HETeamFilterModel *hosp in _menuData) {
        HEFindFiterModel *item = [HEFindFiterModel new];
        item.name = hosp.team_hospital_name;
        item.value = hosp.team_hospital_name;
        item.children = @[].mutableCopy;
        [depts addObject:item];
        
        if (![@"全部" isEqualToString:item.name]) {
            HEFindFiterModel *dept = [HEFindFiterModel new];
            dept.name = @"全部";
            [item.children addObject:dept];
        }
        
        for (HETeamFilterDeptModel *model in hosp.dept) {
            HEFindFiterModel *dept = [HEFindFiterModel new];
            dept.name = model.team_dept_name;
            dept.value = model.team_dept_name;
            [item.children addObject:dept];
        }
    }
    self.menuView.deptList = depts;
    
}

- (void)loadData {
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KMyTeam parameters:@{} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }

        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];

        [self.dataSourceArr addObjectsFromArray:[HETeamModel mj_objectArrayWithKeyValuesArray:response.data]];
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEFindTeamCell *cell = [_tableView dequeueReusableCellWithIdentifier:  @"HEFindTeamCell"];
    cell.model = _dataSourceArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: @"HEFindTeamCell" cacheByIndexPath:indexPath configuration:^(HEFindTeamCell *cell) {
        cell.model = self.dataSourceArr[indexPath.row];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HETeamModel *model = self.dataSourceArr[indexPath.row];
    [self intoTeam:model];
}
 
- (void)intoTeam:(HETeamModel *)model{
    HEIMKitInfo *reciverInfo = [HEIMKitInfo new];
    reciverInfo.infoId = [NSString stringWithFormat:@"%ld",model.teamId];
    reciverInfo.showName = model.teamName;
    
    UserInfo *user = [UserManager sharedUserManager].curUserInfo;
    HEIMKitInfo *owner = [HEIMKitInfo new];
    owner.infoId = user.id;
    owner.showName = user.realname;
    owner.avatarUrlString = user.avatar;
    owner.avatarImage = IMAGE_NAMED(@"defualt_icon");
    
    [[HEIMKit sharedKit] registeUserInfos: @[reciverInfo, owner]];

    HEIMTeamViewController *imVC = [HEIMTeamViewController new];
    imVC.teamId = reciverInfo.infoId;
    imVC.modelType = HEIMUserModelTypeTeamIM;
    [self.navigationController pushViewController:imVC animated:YES];
}

@end
